package org.lanqiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lanqiao.common.CustomException;
import org.lanqiao.entity.Category;
import org.lanqiao.entity.Dish;
import org.lanqiao.entity.Setmeal;
import org.lanqiao.mapper.CategoryMapper;
import org.lanqiao.service.CategoryService;
import org.lanqiao.service.DishService;
import org.lanqiao.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper , Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，删除之前需要判断有无关联的菜品或者套餐，有则无法删除
     */
    @Override
    public void remove(Long id) {
        QueryWrapper<Dish> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("category_id" , id);

        //在菜品表中查询是否有关联的菜品
        int dishCount = dishService.count(queryWrapper1);
        if(dishCount > 0 ){
            //提示业务错误
            throw new CustomException("当前分类已关联菜品，无法删除");
        }

        QueryWrapper<Setmeal> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("category_id", id);
        //在套餐表中查询是否有关联的套餐
        int setmealCount = setmealService.count(queryWrapper2);
        if(setmealCount > 0){
            //提示业务错误
            throw new CustomException("当前分类已关联套餐，无法删除");
        }

        super.removeById(id);
    }
}
